/**
 * @author richt / http://richt.me
 * @author WestLangley / http://github.com/WestLangley
 *
 * W3C Device Orientation control (http://w3c.github.io/deviceorientation/spec-source-orientation.html)
 */
import * as THREE from "three";

export default function DeviceOrientationControls ( object ) {

	var scope = {}; //= this;

	//相机
    scope.object = object;
    scope.object.rotation.reorder( 'YXZ' );

    scope.enabled = true;

    scope.deviceOrientation = {};
    scope.screenOrientation = 0;

    scope.alphaOffset = 0; // radians

	var i = 0

	var ii = 0

	console.log(scope)

	var onDeviceOrientationChangeEvent = function ( event ) {
		// console.log("我被触发了")
		scope.deviceOrientation = event;

	};

	var onScreenOrientationChangeEvent = function () {

		scope.screenOrientation = window.orientation || 0;

	};

	// The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''

	var setObjectQuaternion = function () {
		// console.log("调用")

		var zee = new THREE.Vector3( 0, 0, 1 );

		var euler = new THREE.Euler();

		var q0 = new THREE.Quaternion();

		var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis-


		return function ( quaternion, alpha, beta, gamma, orient ) {

			euler.set( beta, alpha, - gamma, 'YXZ' ); // 'ZXY' for the device, but 'YXZ' for us
			// console.log("euler",euler1._onChangeCallback())

			quaternion.setFromEuler( euler ); // orient the device

			quaternion.multiply( q1 ); // camera looks out the back of the device, not the top

			if ( ii % 1200 == 0) {
				// console.log("euler",euler1)
			}
			ii++
			quaternion.multiply( q0.setFromAxisAngle( zee, - orient ) ); // adjust for screen orientation


		};

	}();

    scope.connect = function () {

		onScreenOrientationChangeEvent(); // run once on load

		window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
		window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );

		scope.enabled = true;

	};

    scope.disconnect = function () {

		window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
		window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );

		scope.enabled = false;

	};

    scope.update = function () {


		if ( scope.enabled === false ) return;

		var device = scope.deviceOrientation;


		if ( device ) {

			var alpha = device.alpha ? THREE.Math.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z

			var beta = device.beta ? THREE.Math.degToRad( device.beta ) : 0; // X'

			var gamma = device.gamma ? THREE.Math.degToRad( device.gamma ) : 0; // Y''

			var orient = scope.screenOrientation ? THREE.Math.degToRad( scope.screenOrientation ) : 0; // O
			if (i % 120 == 0 ) {
				// console.log("scope.object",scope.object)

			}

			i++


			setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient );

		}


	};

    scope.dispose = function () {

		scope.disconnect();

	};

    scope.connect();

    return scope;
};
